<template>
  <div>
    <!--搜索框-->
    <el-form :inline="true" :model="searchForm">
      <el-form-item>
        <el-input v-model="searchForm.name"
                  placeholder="名称"
                  clearable>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getCourseList">搜索</el-button>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="dialogFormVisible = true">新增</el-button>
      </el-form-item>

      <el-form-item>
        <el-popconfirm title="确定要批量删除吗？" @confirm="delHandle(null)">
          <el-button type="danger" slot="reference" :disabled="delBtlStatu">批量删除</el-button>
        </el-popconfirm>
      </el-form-item>
    </el-form>

    <!--多选Tab-->
    <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        stripe
        @selection-change="handleSelectionChange">
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>

      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="课程封面">
              <img :src="props.row.cover" width="100px" height="150px" alt="找不到图片">
            </el-form-item>
            <el-form-item label="课程介绍">
              <span>{{ props.row.info }}</span>
            </el-form-item>
            <el-form-item label="学分">
              <span>{{ props.row.credit }}</span>
            </el-form-item>
            <el-form-item label="学时">
              <span>{{ props.row.hour }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>

      <el-table-column
          prop="code"
          label="课程代码"
          width="120">
      </el-table-column>
      <el-table-column
          prop="name"
          label="课程名称"
          width="120">
      </el-table-column>
      <el-table-column
          prop="type"
          label="课程类别"
          width="120">
      </el-table-column>

      <el-table-column
          label="课程负责人"
          show-overflow-tooltip>
        <template slot-scope="scope">
          <el-tooltip placement="top">
            <div slot="content" v-if="scope.row.teacher != null">{{ scope.row.teacher.code }}</div>
            <el-tag size="medium">{{ scope.row.teacher != null ? scope.row.teacher.name : '暂无课程负责人' }}</el-tag>
          </el-tooltip>
        </template>
      </el-table-column>

      <el-table-column
          label="课程支撑"
          show-overflow-tooltip>
        <template slot-scope="scope">
          <el-tooltip placement="top" v-for="point in scope.row.pointList">
            <div slot="content">{{ point.remark }}</div>
            <el-tag size="medium">{{ point.name }}</el-tag>
          </el-tooltip>

        </template>
      </el-table-column>

      <el-table-column
          label="操作"
          width="250">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" @click="editHandle(scope.row)">编辑</el-button>
          <el-divider direction="vertical"></el-divider>
          <el-popconfirm title="确定要删除这条记录吗？" @confirm="delHandle(scope.row.id)">
            <el-button type="danger" icon="el-icon-delete" slot="reference">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!--分页-->
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes, prev, pager, next, jumper"
        :page-sizes="[10, 20, 50, 100]"
        :current-page="current"
        :page-size="size"
        :total="total">
    </el-pagination>

    <!--新增对话框-->
    <el-dialog title="课程信息" :visible.sync="dialogFormVisible" width="600px" @closed="resetForm('editForm')">
      <el-form :model="editForm" :rules="editFormRules" ref="editForm">

        <el-form-item label="课程代码" prop="code" label-width="100px">
          <el-input v-model="editForm.code" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="课程名称" prop="name" label-width="100px">
          <el-input v-model="editForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="课程介绍" label-width="100px">
          <el-input type="textarea" v-model="editForm.info"></el-input>
        </el-form-item>

        <el-form-item label="课程封面" prop="cover" label-width="100px">
          <el-input v-model="editForm.cover" autocomplete="off"></el-input>
          <img :src="editForm.cover" width="50px" height="100px" alt="找不到图片">
        </el-form-item>

        <!--<el-form-item label="期末比值" prop="endRatio" label-width="100px">-->
        <!--  <el-input-number v-model="editForm.endRatio" :precision="2" :step="0.1" :min="0" :max="1"></el-input-number>-->
        <!--</el-form-item>-->

        <el-form-item label="课程负责人" label-width="100px">
          <el-select v-model="editForm.teacherId" placeholder="请选择活动区域">
            <el-option :label="item.name" :value="item.id" v-for="item in teacherData"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="课程支撑（指标点）" label-width="100px">
          <el-select v-model="editForm.pointIds" multiple placeholder="请选择指标点" @change="$forceUpdate()">
            <el-option :label="item.name" :value="item.id" v-for="item in pointData"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="学分" prop="credit" label-width="100px">
          <el-input v-model="editForm.credit" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="学时" prop="hour" label-width="100px">
          <el-input v-model="editForm.hour" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="课程类别" prop="type" label-width="100px">
          <el-input v-model="editForm.type" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="适用专业" prop="major" label-width="100px">
          <el-input v-model="editForm.major" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('editForm')">取 消</el-button>
        <el-button type="primary" @click="submitEditForm('editForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Course",
  data() {
    return {
      searchForm: {},
      delBtlStatu: true,
      dialogFormVisible: false,
      total: 0,
      size: 10,
      current: 1,
      editForm: {
        pointIds: []
      },
      teacherData: [],
      pointData: [],
      tableData: [],
      editFormRules: {
        name: [
          {required: true, message: '请输入教师姓名', trigger: 'blur'}
        ],
        code: [
          {required: true, message: '请输入教师编码', trigger: 'blur'}
        ]
      },
      multipleSelection: [],
      permDialogVisible: false,
      permForm: {},
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  created() {
    this.getCourseList()
    this.$axios.get("/teacher/list").then(res => {
      this.teacherData = res.data.data.records
    })

    this.$axios.get("/point", {
      params: {
        current: 1,
        size: 10000
      }
    }).then(res => {
      this.pointData = res.data.data.records
    })
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val;
      this.delBtlStatu = val.length === 0
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.size = val
      this.getCourseList()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.current = val
      this.getCourseList()
    },
    resetForm(formName) {
      this.editForm = {
        id: null,
        pointIds: []
      }
      this.dialogFormVisible = false
    },
    handleClose() {
      this.resetForm('editForm')
    },
    getCourseList() {
      this.$axios.get("/course", {
        params: {
          name: this.searchForm.name,
          current: this.current,
          size: this.size
        }
      }).then(res => {
        this.tableData = res.data.data.records
        this.size = res.data.data.size
        this.current = res.data.data.current
        this.total = res.data.data.total
      })
    },
    submitEditForm(formName) {
      console.log(this.editForm)
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios.post('/course', this.editForm)
              .then(res => {
                this.getCourseList()
                console.log(res.data)
                this.resetForm(formName)
                this.$message({
                  showClose: true,
                  message: '恭喜你，操作成功',
                  type: 'success',
                });
              })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    editHandle(data) {
      // console.log(data)
      this.editForm = data
      if (data.pointList) {
        console.log(data.pointList)
        let pointIds = [];
        for (let i = 0; i < data.pointList.length; i++) {
          pointIds.push(data.pointList[i].id)
        }
        this.editForm.pointIds = pointIds
      }
      this.dialogFormVisible = true
    },
    delHandle(id) {
      var ids = []
      if (id) {
        ids.push(id)
      } else {
        // 批量删除
        this.multipleSelection.forEach(row => {
          ids.push(row.id)
        })
      }
      this.$axios.delete("/course", {
        data: ids
      }).then(res => {
        this.$message({
          showClose: true,
          message: '恭喜你，操作成功',
          type: 'success',
        });
        // 刷新列表
        this.getCourseList()
      })
    },
  }
}
</script>

<style scoped>
.el-pagination {
  float: right;
  margin-top: 10px;
}

.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>